#include "fptree.h"

FPTree::FPTree()
{
    // Create root of tree
    root = new FPTreeNode;
    root->setPage("root");
    root->setQuantity(-1);
    root->parent = 0;

    // Assign memory for global node pointers list
    globalNodePointersList = new QList<FPTreeNode *>();
}

void FPTree::addPages(QStringList *pages)
{
    // This method add list of pages to the FPTree
    QString currentPage;
    FPTreeNode *currentNode = root;

    //qDebug() << "address of root's children" << root->children << "====================================";

    for(int i = 0; i < pages->size(); ++i)
    {
        currentPage = pages->at(i);
        //qDebug() << "Adding current page" << currentPage << root->parent << "========================================";

        bool createNodeInChildrenList = true;

        // Check current node has children with proper page
        if(currentNode->children == 0)
        {
            //qDebug() << "Creating child in " << i << "loop and current node is" << currentNode->getPage() << currentNode->parent;
            // Current node has not any child, create it
            currentNode->children = new QList<FPTreeNode *>();

            // Create node
            FPTreeNode *node = new FPTreeNode;
            node->setPage(currentPage);
            node->setQuantity(1);
            node->children = 0;
            node->parent = currentNode;

            // Add new node to global nodes list
            globalNodePointersList->append(node);

            // Append to current node
            currentNode->children->append(node);

            // And change current node
            currentNode = node;
            //qDebug("\tDBG: false on first condition");
        }
        else
        {
            // Current node has a children so app has to check out
            //  Maybe node has page which is needed
            for(int j = 0; j < currentNode->children->size(); ++j)
            {
                if(currentNode->children->at(j)->getPage() == currentPage)
                {
                    // Woohoo! Page exists in current node
                    int q = currentNode->children->at(j)->getQuantity();
                    currentNode->children->at(j)->setQuantity(q + 1);
                    qDebug() << "WooHoo! Incrementing node" << currentNode->children->at(j)->getPage() << currentNode->children->at(j)->getQuantity();

                    // Change current node
                    currentNode = currentNode->children->at(j);

                    //qDebug() << "\tDGB: Page exists on node" << currentPage << currentNode->parent;
                    createNodeInChildrenList = false;
                    break;
                }
                /*else
                {
                    // Page does not exists in current branch
                    //  Create it!
                    //currentNode->children = new QList<FPTreeNode *>();      // Fuckin' mistake!

                    // Create node
                    FPTreeNode *node = new FPTreeNode;
                    node->setPage(currentPage);
                    node->setQuantity(1);
                    node->parent = currentNode;
                    node->children = 0;

                    // Add new node to global nodes list
                    globalNodePointersList->append(node);

                    // Append to current node
                    currentNode->children->append(node);

                    // And finally change current node
                    currentNode = node;

                    //qDebug() << "\tDGB: Page doesnt existed on node" << root->parent;

                    break;
                }*/
            }

            if(createNodeInChildrenList == true)
            {
                // Create node
                FPTreeNode *node = new FPTreeNode;
                node->setPage(currentPage);
                node->setQuantity(1);
                node->parent = currentNode;
                node->children = 0;

                // Add new node to global nodes list
                globalNodePointersList->append(node);

                // Append to current node
                currentNode->children->append(node);

                // And finally change current node
                currentNode = node;
            }
            //qDebug() << "loop" << i;
        }

        //qDebug() << "current page" << currentPage;
    }
}

void FPTree::showTree()
{
    qDebug() << "showing tree";
    FPTreeNode *currentNode = 0;
    int maxDeepness = 6;

    // Let's browse all nodes and start showing only leafs
    for(int i = 0; i < globalNodePointersList->size(); ++i)
    {
        // Is current node a leaf?
        if(globalNodePointersList->at(i)->children == 0)
        {
            currentNode = globalNodePointersList->at(i);
            // Variable for modyfing deepnes of branches
            int i = 0;
            // Yes, it is! Start drawing...
            while(true)
            {
                if(currentNode == 0)
                    break;

                QString t = "";
                for(int j = i; j > 0; --j)
                    t += "  ";

                qDebug() << t << currentNode->getPage() << currentNode->getQuantity();
                currentNode = currentNode->parent;

                i++;
            }
        }
    }
}

// Debug only
void FPTree::debug()
{
    //qDebug() << "Preview of root";

    //qDebug() << *(root->children);

    FPTreeNode *currentNode = root;

    while(true)
    {
        if(currentNode == 0)
            break;

        qDebug() << currentNode->getPage() << currentNode->getQuantity() << currentNode;
        qDebug() << "size: " << currentNode->children->size();
        //currentNode = currentNode->children->at(0);
        currentNode = currentNode->children->first();
    }
}
